package com.project.decoration.controller.list;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationPriceList;
import com.project.decoration.domain.DecorationPriceServer;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationPriceListService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 套餐费用明细controller
 * @date 2025/02/13 10:04
 */
@Controller
@ResponseBody
@RequestMapping("list")
public class DecorationPriceListController {

    @Autowired
    private DecorationPriceListService decorationPriceListService;

    /** 分页获取套餐费用明细 */
    @PostMapping("getDecorationPriceListPage")
    public Result getDecorationPriceListPage(@RequestBody DecorationPriceList decorationPriceList) {
        Page<DecorationPriceList> page = new Page<>(decorationPriceList.getPageNumber(),decorationPriceList.getPageSize());
        QueryWrapper<DecorationPriceList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(StringUtils.isNotBlank(decorationPriceList.getPriceId()),DecorationPriceList::getPriceId,decorationPriceList.getPriceId())
                .like(StringUtils.isNotBlank(decorationPriceList.getName()),DecorationPriceList::getName,decorationPriceList.getName());
        Page<DecorationPriceList> decorationPriceListPage = decorationPriceListService.page(page, queryWrapper);
        return Result.success(decorationPriceListPage);
    }

    @GetMapping("getDecorationPriceListByPriceId")
    public Result getDecorationPriceListByPriceId(@RequestParam("priceId") String priceId) {
        QueryWrapper<DecorationPriceList> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(DecorationPriceList::getPriceId,priceId);
        List<DecorationPriceList> serverList = decorationPriceListService.list(queryWrapper);
        return Result.success(serverList);
    }

    /** 根据id获取套餐费用明细 */
    @GetMapping("getDecorationPriceListById")
    public Result getDecorationPriceListById(@RequestParam("id")String id) {
        DecorationPriceList decorationPriceList = decorationPriceListService.getById(id);
        return Result.success(decorationPriceList);
    }

    /** 保存套餐费用明细 */
    @PostMapping("saveDecorationPriceList")
    public Result saveDecorationPriceList(@RequestBody DecorationPriceList decorationPriceList) {
        boolean save = decorationPriceListService.save(decorationPriceList);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑套餐费用明细 */
    @PostMapping("editDecorationPriceList")
    public Result editDecorationPriceList(@RequestBody DecorationPriceList decorationPriceList) {
        boolean save = decorationPriceListService.updateById(decorationPriceList);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除套餐费用明细 */
    @GetMapping("removeDecorationPriceList")
    public Result removeDecorationPriceList(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                decorationPriceListService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("套餐费用明细id不能为空！");
        }
    }

}
